package com.hbnu.client;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class WordClient {
    public static void main(String[] args) {
        try (Socket socket = new Socket("localhost", 8080);
             BufferedReader in = new BufferedReader(
                     new InputStreamReader(socket.getInputStream(),"UTF-8"));
             PrintWriter out = new PrintWriter(
                     new OutputStreamWriter(socket.getOutputStream(),"UTF-8"), true);
             Scanner scanner = new Scanner(System.in,"UTF-8")) {

            System.out.println("已连接到背单词服务器...");

            String response;
            while ((response = in.readLine()) != null) {
                System.out.println(response);

                if (response.contains("再见")) {
                    break;
                }
                if (response.contains("请选择") ||
                        response.contains("请输入") ||
                        response.trim().endsWith("：") ||
                        response.trim().endsWith(":") ||
                        response.contains("选择操作")) {
                    String input = scanner.nextLine();
                    out.println(input);
                }
            }

        } catch (IOException e) {
            System.err.println("客户端错误：" + e.getMessage());
        }
    }
}
